package com.formulas.component.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serial;

/**
 * 页面查询参数
 * @author yiwannuofulasi
 * @date 2023/6/2 10:57
 */
public abstract class PageQuery extends Query {

    @Serial
    private static final long serialVersionUID = 1L;

    /**升序**/
    public static final String ASC = "ASC";
    /**降序**/
    public static final String DESC = "DESC";
    /**默认页面记录数**/
    private static final int DEFAULT_PAGE_SIZE = 10;
    /**分页大小**/
    @Schema(description = "分页大小（默认10条，建议控制最大在100条以内，分页limit值越大影响查询速度）")
    private int pageSize = DEFAULT_PAGE_SIZE;
    /**分页下标**/
    @Schema(description = "分页下标（起始页，默认第一页）")
    private int pageIndex = 1;
    /**排序属性**/
    @Schema(description = "排序属性（动态设置排序字段）")
    private String orderBy;
    /**排序方向降序**/
    @Schema(description = "排序方向（动态设置排序方向(DESC或者ASC)）")
    private String orderDirection = DESC;
    /**分组字段**/
    @Schema(description = "分组字段（动态设置分组字段）")
    private String groupBy;
    /**是否需要总数**/
    @Schema(description = "是否需要总数（默认需要（true或者false））")
    private boolean needTotalCount = true;

    /**
     * 获取分页下标
     * @return int
     */
    public int getPageIndex() {
        if (pageIndex < 1) {
            return 1;
        }
        return pageIndex;
    }

    /**
     * 设置分页下标
     * @param pageIndex int
     * @return PageQuery
     */
    public PageQuery setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
        return this;
    }

    /**
     * 获取分页大小
     * @return int
     */
    public int getPageSize() {
        if (pageSize < 1) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return pageSize;
    }

    /**
     * 设置分页大小
     * @param pageSize int
     * @return PageQuery
     */
    public PageQuery setPageSize(int pageSize) {
        if (pageSize < 1) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        this.pageSize = pageSize;
        return this;
    }

    /**
     * 获取偏移量
     * @return int
     */
    public int getOffset() {
        return (getPageIndex() - 1) * getPageSize();
    }

    /**
     * 获取排序
     * @return String
     */
    public String getOrderBy() {
        return orderBy;
    }

    /**
     * 设置排序
     * @param orderBy String
     * @return PageQuery
     */
    public PageQuery setOrderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    /**
     * 获取排序方向
     * @return String
     */
    public String getOrderDirection() {
        return orderDirection;
    }

    /**
     * 设置排序方向
     * @param orderDirection String
     * @return PageQuery
     */
    public PageQuery setOrderDirection(String orderDirection) {
        if (ASC.equalsIgnoreCase(orderDirection) || DESC.equalsIgnoreCase(orderDirection)) {
            this.orderDirection = orderDirection;
        }
        return this;
    }

    /**
     * 获取分组
     * @return String
     */
    public String getGroupBy() {
        return groupBy;
    }

    /**
     * 设置分组
     * @param groupBy String
     */
    public void setGroupBy(String groupBy) {
        this.groupBy = groupBy;
    }

    /**
     * 判断是否需要总数
     * @return boolean
     */
    public boolean isNeedTotalCount() {
        return needTotalCount;
    }

    /**
     * 设置是否需要总数
     * @param needTotalCount boolean
     */
    public void setNeedTotalCount(boolean needTotalCount) {
        this.needTotalCount = needTotalCount;
    }
}
